<?php
/**
 * User: Do
 * Date: 7/4/14
 * Time: 11:33 AM
 */
class Model_users extends Model{
    function __construct(){
        parent::__construct();
    }

    //Function
    public function getUsers($id){
        try{
            $sth = $this->db->prepare("select * from users");
            $sth->execute(array(
                ':uid' => $id
            ));
            return $sth->fetchAll();
        }
        catch (Exception $e){
            throw new Exception("Saving error! Try again!", 201);
        }
    }

    public function getUserByUsername($username){
        try{
            $sth = $this->db->prepare("select * from users where username like :username");
            $sth->execute(array(
                ':username' => $username
            ));
            return $sth->fetchAll();
        }
        catch (Exception $e){
            throw new Exception("Saving error! Try again!", 201);
        }
    }

    public function getUserByEmail($email){
        try{
            $sth = $this->db->prepare("select username from users where email like :email");
            $sth->execute(array(
                ':email' => $email
            ));
            return $sth->fetchAll();
        }
        catch (Exception $e){
            throw new Exception("Saving error! Try again!", 201);
        }
    }

    public function addUser($username, $password, $email, $address, $sex, $birthday, $salary, $children){
        try{
            $sth = $this->db->prepare("insert into users(`username`, `password`,`email`, `address`, `sex`, `birthday`, `salary`, `children`)
            values( :username, :password, :email, :address, :sex, :birthday, :salary, :children)");
            $sth->execute(array(
                ':username' => $username,
                ':password' => $password,
                ':email' => $email,
                ':address' => $address,
                ':sex' => $sex,
                ':birthday' => $birthday,
                ':salary' => $salary,
                ':children' => $children
            ));
            return $this->db->lastInsertId();
        }
        catch (Exception $e){
            throw new Exception("Saving error! Try again!", 201);
        }
    }

}